﻿using System;
using System.ServiceModel;
using KillBill.Business.Entities;
using KillBill.Business.Entities.ListEntities;
using KillBill.Common.View;
using KillBill.Presentation.Common;
using KillBill.Presentation.Modules.Products.IView;
using KillBill.Presentation.Modules.Products.ProductsServiceReference;
using KillBill.Presentation.Modules.Products.SuppliersServiceReference;
using KillBill.Services.DTO;

namespace KillBill.Presentation.Modules.Products.Presenter
{
    public class ProductAddEditPresenter
    {
        //Service
        private ProductsServiceClient _productsService;
        private SuppliersServiceClient _suppliersService;

        public IProductAddEdit View { get; set;}

        public ProductAddEditPresenter(IProductAddEdit iProductAddEdit)
        {
            View = iProductAddEdit;
            _productsService = new ProductsServiceClient();
            _suppliersService = new SuppliersServiceClient();
        }

        public void GetProviders()
        {
            try
            {
                var result = _suppliersService.GetProviders(Context.SessionID);
                if (!result.Success && result.Messages != null && result.Messages.Count > 0)
                    View.ShowMessage(View.Text, result.Messages[0], MessageOptions.Ok,
                                         MessageIcon.Error);
                else
                    View.ProviderList = result.Providers;
                
            }
            catch (Exception ex)
            {
                View.ShowMessage(View.Text, ex.Message, MessageOptions.Ok,
                                 MessageIcon.Error);
            }
            
        }

        public void Save()
        {
            try
            {
                KillBillDTO result;
                if (View.IsEditing)
                {
                    result = _productsService.UpdateProduct(View.Product, Context.SessionID);
                    if (result.Success && result.Messages != null && result.Messages.Count > 0)
                    {
                        View.ShowMessage(View.Text, result.Messages[0], MessageOptions.Ok,
                                         MessageIcon.Information);
                    }
                    else if (result.Messages != null && result.Messages.Count > 0)
                    {
                        View.ShowMessage(View.Text, result.Messages[0], MessageOptions.Ok,
                                         MessageIcon.Error);
                    }
                }
                else
                {
                    result = _productsService.AddProduct(View.Product, Context.SessionID);
                    if (result.Success && result.Messages != null && result.Messages.Count > 0)
                    {
                        View.ShowMessage(View.Text, result.Messages[0], MessageOptions.Ok,
                                         MessageIcon.Information);
                    }
                    else if (result.Messages != null && result.Messages.Count > 0)
                    {
                        View.ShowMessage(View.Text, result.Messages[0], MessageOptions.Ok,
                                         MessageIcon.Error);
                    }
                }
            }
            catch (EndpointNotFoundException)
            {
                View.ShowConnectionErrorMessage(View.Text);
            }
            catch (Exception ex)
            {
                View.ShowMessage(View.Text, ex.Message, MessageOptions.Ok,
                                 MessageIcon.Error);
            }
        }
    }
}
